package com.tenebrous.jestic.controller.server;

import com.rain.ops.sdk.dto.ApiResult;
import com.tenebrous.jestic.context.Response;
import com.tenebrous.jestic.controller.SupperController;
import com.tenebrous.jestic.entity.model.request.DecryptionRequest;
import com.tenebrous.jestic.entity.model.request.JesticAuthorityRequest;
import com.tenebrous.jestic.entity.model.request.ServerHearBeatRequest;
import com.tenebrous.jestic.entity.model.response.AbstractResponse;
import com.tenebrous.jestic.service.server.AuthServerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description:
 * @Project: jestic-server
 * @Package: com.tenebrous.jestic.controller.server
 * @Author: zhangya
 * @CreateTime: 2023-06-02  16:25
 * @Version: 1.0.0
 */
@Validated
@RestController
@RequestMapping("/jestic/remote/v1")
public class JesticRemoteController extends SupperController {

    @Autowired
    private AuthServerService authServerService;

    @PostMapping("/authority")
    public ApiResult authority(@RequestBody JesticAuthorityRequest request) {
        return authServerService.authority(request);
    }

    @PostMapping("/decrypt")
    public ApiResult<String> decrypt(@RequestBody DecryptionRequest request, HttpServletRequest httpRequest) {
        String token = httpRequest.getHeader("Authorization");
        String instanceId = httpRequest.getHeader("instanceId");
        return authServerService.decrypt(request, token, instanceId);
    }

    @PostMapping("/heartBeat")
    public ApiResult<Boolean> heartBeat(@RequestBody ServerHearBeatRequest request, HttpServletRequest httpRequest) {
        String token = httpRequest.getHeader("Authorization");
        String instanceId = httpRequest.getHeader("instanceId");
        return authServerService.heartBeat(request, token, instanceId);
    }
}
